<?php

namespace app\bh\controller;

use app\bh\model\QuyuModel;
use app\bh\model\UserModel;
use think\App;
use think\Controller;
use think\Request;
use think\Response;

class BaseAdminController extends Controller
{
    private static $aes_key = '12aasa21dakqnbzv';
    static $user;
    static $qx;

    public function __construct(App $app = null, Request $request)
    {
        parent::__construct($app);
        $token = $request->header('token');
        $to_user = self::decrypt($token);
        $this->yz_set($to_user);
    }

    public function yz_set($to_user)
    {

        //动态设置权限数组 和 区域
        $user = UserModel::find($to_user['id']);
        self::$user = $user;
        self::$qx = $user->roles;
    }

    //成功返回
    public function SuccessReturn($msg = '', $data = [])
    {
        return json(['code' => 200, 'msg' => $msg, 'data' => $data]);
    }

    //失败返回
    public function ErrorReturn($code = '', $msg = '', $data = [])
    {
        return json(['code' => $code, 'msg' => $msg, 'data' => $data]);
    }

    /**
     * 加密
     * @param string $str 要加密的数据
     * @return bool|string   加密后的数据
     */
    static public function encrypt($str)
    {

//        $data = openssl_encrypt($str, 'AES-128-ECB', self::$aes_key, OPENSSL_RAW_DATA);
        $data = base64_encode($str);
        return $data;
    }

    /**
     * 解密
     * @param string $str 要解密的数据
     * @return string        解密后的数据
     */
    static public function decrypt($str)
    {

//        $decrypted = openssl_decrypt(base64_decode($str), 'AES-128-ECB', self::$aes_key, OPENSSL_RAW_DATA);
        return json_decode(base64_decode($str), true);

    }

}